package com.lin.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.mapper.DishMapper;
import com.lin.mapper.FormMapper;
import com.lin.mapper.Plus.ShoppingMapperPlus;
import com.lin.mapper.ShoppingMapper;
import com.lin.mapper.UserMapper;
import com.lin.pojo.Dish;
import com.lin.pojo.Shopping;
import com.lin.response.CommonResp;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingService extends ServiceImpl<ShoppingMapperPlus, Shopping> {
    @Autowired
    private ShoppingMapper shoppingMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FormMapper formMapper;

    @Autowired
    private DishMapper dishMapper;

    public CommonResp page(Integer pageNum,Integer pageSize,String username,String dish){
        pageNum=(pageNum-1)*pageSize;
        dish = "%"+dish+"%";
        Integer total = shoppingMapper.SelectCount_ByDish(username,dish);           //1.计算总条数
        Integer totalprice = shoppingMapper.Select_TotalPrice(username);            //2.计算当前购物车总价
        List<Shopping> data = shoppingMapper.SelectPage_ByName(pageNum,pageSize,username,dish);     //3.放回购物车内容
        Map<String,Object> res = new HashMap<>();
        res.put("data",data);
        res.put("total" , total);
        res.put("totalprice",totalprice);
        CommonResp< Map<String,Object> > commonResp = new CommonResp();
        commonResp.setContent(res);
        commonResp.setSuccess(true);
        commonResp.setMessage("查找成功。");
        return commonResp;
    }
    public CommonResp first_add(Dish dish, String username){
//        System.out.println(dish);
        Map<String,Object> res = new HashMap<>();
        CommonResp< Map<String,Object> > commonResp = new CommonResp();
        Integer flag = shoppingMapper.SelectExit_ByName(username,dish.getName());
        if(flag >0){
            commonResp.setContent(res);
            commonResp.setSuccess(false);
            commonResp.setMessage("该菜品已在购物车中，可前往购物车调整数量。");
        }
        else{
            shoppingMapper.Insert(username,dish.getName(),dish.getPrice(),1);
            commonResp.setContent(res);
            commonResp.setSuccess(true);
            commonResp.setMessage("加入购物车成功。");
        }
        return commonResp;
    }

    public CommonResp changeCount(Shopping shopping){
        String username = shopping.getUsername();
        String dish = shopping.getDish();
        Integer count = shopping.getCount();
        Integer flag;
        if(shopping.getCount() == 0){
            flag = shoppingMapper.Delete(username,dish);
        }
        else{
            flag = shoppingMapper.UpdateCount(count,username,dish);
        }
        CommonResp<String> commonResp = new CommonResp();
        if(flag>0){
            commonResp.setContent(null);
            commonResp.setSuccess(true);
            commonResp.setMessage("修改数量或删除成功。");
        }
        else{
            commonResp.setContent(null);
            commonResp.setSuccess(false);
            commonResp.setMessage("修改数量或删除失败。");
        }
        return commonResp;
    }

    public CommonResp pay(Integer tableid,
                          String username,
                          String detail,
                          Integer totalPrice){
        Integer cur_Money = userMapper.SelectMoney(username);
        CommonResp<String> commonResp = new CommonResp<>();
        if(cur_Money >= totalPrice){
            cur_Money -= totalPrice;
            formMapper.Insert(tableid,username,detail,totalPrice);     //1.新增订单
            userMapper.UpdateMoney(username,cur_Money);                 //2.更新余额
            List<Shopping> data = shoppingMapper.SelectPage_ByName(0,100,username,"%");
            for(Shopping s:data){                       //3.修改每个菜品的受欢迎度
                Integer cur_count = dishMapper.SelectPopularity(s.getDish());
                cur_count += s.getCount();              //把“受欢迎程度” + 下单数量
                dishMapper.UpdatePopularity(s.getDish(),cur_count);
            }
            shoppingMapper.Delete_All(username);                        //4.清空购物车
            commonResp.setContent(null);
            commonResp.setMessage("支付成功。");
            commonResp.setSuccess(true);
        }
        else {
            commonResp.setContent(null);
            commonResp.setMessage("您的余额不足，请前往充值。");
            commonResp.setSuccess(false);
        }
        return  commonResp;
    }

    public CommonResp remove(Shopping shopping){
        String username = shopping.getUsername();
        String dish = shopping.getDish();
        Integer flag = shoppingMapper.Delete(username,dish);
        CommonResp<String> commonResp = new CommonResp<>();
        if(flag > 0){
            commonResp.setContent(null);
            commonResp.setMessage("移除成功");
            commonResp.setSuccess(true);
        }
        else {
            commonResp.setContent(null);
            commonResp.setMessage("移除失败");
            commonResp.setSuccess(false);
        }
        return commonResp;
    }
}
